<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ecg_qc.ecg_qc &mdash; ecg_qc 1.0.0 documentation</title>
      <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> ecg_qc
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../ecg_qc.html">API documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../references.html">References</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">ecg_qc</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../index.html">Module code</a> &raquo;</li>
      <li>ecg_qc.ecg_qc</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for ecg_qc.ecg_qc</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">ecg_qc.sqi_computing.sqi_rr_intervals</span> <span class="kn">import</span> <span class="n">csqi</span><span class="p">,</span> <span class="n">qsqi</span>
<span class="kn">from</span> <span class="nn">ecg_qc.sqi_computing.sqi_frequency_distribution</span> <span class="kn">import</span> <span class="n">ssqi</span><span class="p">,</span> <span class="n">ksqi</span>
<span class="kn">from</span> <span class="nn">ecg_qc.sqi_computing.sqi_power_spectrum</span> <span class="kn">import</span> <span class="n">bassqi</span><span class="p">,</span> <span class="n">psqi</span>
<span class="kn">from</span> <span class="nn">ecg_qc.utilities.type_checking</span> <span class="kn">import</span> <span class="n">check_type_ecg</span>
<span class="kn">from</span> <span class="nn">ecg_qc.utilities.model_loader</span> <span class="kn">import</span> <span class="n">load_model</span>
<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="n">StandardScaler</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>


<div class="viewcode-block" id="EcgQc"><a class="viewcode-back" href="../../ecg_qc.html#ecg_qc.ecg_qc.EcgQc">[docs]</a><span class="k">class</span> <span class="nc">EcgQc</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class determines the quality of an ECG segment, usually lasting</span>
<span class="sd">    several seconds. It computes SQIs (Signal Quality Indicator) and use them</span>
<span class="sd">    in a pre-trained model to predict the quality:</span>
<span class="sd">        * 1 : good quality</span>
<span class="sd">        * 0 : bad quality</span>

<span class="sd">    Attributes</span>
<span class="sd">    ----------</span>
<span class="sd">    model_file : str</span>
<span class="sd">        Trained model to load to predict quality. Can be the name of included</span>
<span class="sd">        pre-trained model or a path to an other model.</span>
<span class="sd">    sampling_frequency : int</span>
<span class="sd">        Sampling frequency of the input ECG signal. Used for several SQI</span>
<span class="sd">        computing</span>
<span class="sd">    normalized : bool</span>
<span class="sd">        If True, will normalise input ecg signal</span>

<span class="sd">    Methods</span>
<span class="sd">    -------</span>
<span class="sd">    compute_sqi_scores(ecg_signal)</span>
<span class="sd">        Computes SQIs from an ECG signal segment</span>
<span class="sd">    predict_quality(sqi_scores)</span>
<span class="sd">        From a list of SQIs, predict the quality of a related ECG segment</span>
<span class="sd">    get_signal_quality(ecg_signal)</span>
<span class="sd">        From an ECG signal segment, directly returns the quality</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">model_file</span><span class="o">=</span><span class="s1">&#39;rfc_norm_2s.pkl&#39;</span><span class="p">,</span>
                 <span class="n">sampling_frequency</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">256</span><span class="p">,</span>
                 <span class="n">normalized</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">):</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">load_model</span><span class="p">(</span><span class="n">model_file</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sampling_frequency</span> <span class="o">=</span> <span class="n">sampling_frequency</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">normalized</span> <span class="o">=</span> <span class="n">normalized</span>

<div class="viewcode-block" id="EcgQc.compute_sqi_scores"><a class="viewcode-back" href="../../ecg_qc.html#ecg_qc.ecg_qc.EcgQc.compute_sqi_scores">[docs]</a>    <span class="k">def</span> <span class="nf">compute_sqi_scores</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                           <span class="n">ecg_signal</span><span class="p">:</span> <span class="nb">list</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        From an ECG Signal segment, computes 6 SQI scores (q_sqi, c_sqi, s_sqi,</span>
<span class="sd">        k_sqi, p_sqi, bas_sqi)</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        ecg_signal : list</span>
<span class="sd">            Input ECG signal</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sqi_scores : list</span>
<span class="sd">            SQI scores related to input ECG segment</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">ecg_signal</span> <span class="o">=</span> <span class="n">check_type_ecg</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">normalized</span><span class="p">:</span>
            <span class="n">ecg_signal</span> <span class="o">=</span> <span class="n">StandardScaler</span><span class="p">()</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span>
                <span class="n">ecg_signal</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>

        <span class="n">q_sqi_score</span> <span class="o">=</span> <span class="n">qsqi</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sampling_frequency</span><span class="p">)</span>
        <span class="n">c_sqi_score</span> <span class="o">=</span> <span class="n">csqi</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sampling_frequency</span><span class="p">)</span>

        <span class="n">s_sqi_score</span> <span class="o">=</span> <span class="n">ssqi</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">)</span>
        <span class="n">k_sqi_score</span> <span class="o">=</span> <span class="n">ksqi</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">)</span>

        <span class="n">p_sqi_score</span> <span class="o">=</span> <span class="n">psqi</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sampling_frequency</span><span class="p">)</span>
        <span class="n">bas_sqi_score</span> <span class="o">=</span> <span class="n">bassqi</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sampling_frequency</span><span class="p">)</span>

        <span class="n">sqi_scores</span> <span class="o">=</span> <span class="p">[[</span><span class="n">q_sqi_score</span><span class="p">,</span> <span class="n">c_sqi_score</span><span class="p">,</span> <span class="n">s_sqi_score</span><span class="p">,</span>
                       <span class="n">k_sqi_score</span><span class="p">,</span> <span class="n">p_sqi_score</span><span class="p">,</span> <span class="n">bas_sqi_score</span><span class="p">]]</span>

        <span class="k">return</span> <span class="n">sqi_scores</span></div>

<div class="viewcode-block" id="EcgQc.predict_quality"><a class="viewcode-back" href="../../ecg_qc.html#ecg_qc.ecg_qc.EcgQc.predict_quality">[docs]</a>    <span class="k">def</span> <span class="nf">predict_quality</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sqi_scores</span><span class="p">:</span> <span class="nb">list</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        From an ECG segment SQI scores, use pre-trained model to compute</span>
<span class="sd">        the quality of the signal.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        sqi_scores : list(list)</span>
<span class="sd">            SQI scores related to input ECG segment</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        prediction : int</span>
<span class="sd">            The signal quality predicted by the model</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sqi_scores</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">sqi_scores</span><span class="p">)</span>
        <span class="n">prediction</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">sqi_scores</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">prediction</span></div>

<div class="viewcode-block" id="EcgQc.get_signal_quality"><a class="viewcode-back" href="../../ecg_qc.html#ecg_qc.ecg_qc.EcgQc.get_signal_quality">[docs]</a>    <span class="k">def</span> <span class="nf">get_signal_quality</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                           <span class="n">ecg_signal</span><span class="p">:</span> <span class="nb">list</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        From an ECG segment signal, use pre-trained model to compute</span>
<span class="sd">        the quality of the signal. This method is a shortcut to using</span>
<span class="sd">        compute_sqi_scores then predict quality.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        ecg_signal : list</span>
<span class="sd">            Input ECG signal</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        prediction : int</span>
<span class="sd">            The signal quality predicted by the model</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sqi_scores</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">compute_sqi_scores</span><span class="p">(</span><span class="n">ecg_signal</span><span class="p">)</span>
        <span class="n">quality_predicted</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">predict_quality</span><span class="p">(</span><span class="n">sqi_scores</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">quality_predicted</span></div></div>
</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2021, Aura Healthcare.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>